/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

//Not perfect.. images skip when scrolling
public class SlidingPictureBrowser extends TraversableWidget {

	private final class Ani extends TimerTask {
		private byte dir;

		private Ani(byte dir) {
			this.dir = dir;
		}

		private void checkIndex() {
			if (currentIndex < images.size() - 1) {
				currentIndex++;
			} else {
				currentIndex = 0;
			}
			nextIndex = (currentIndex != images.size() - 1) ? currentIndex + 1 : 0;
			currImageX = 0;
		}

		public void checkIndex2() {
			if (currentIndex > 0) {
				currentIndex--;
			} else {
				currentIndex = images.size() - 1;
			}
			nextIndex = (currentIndex == 0) ? images.size() - 1 : currentIndex - 1;
			currImageX = 0;
		}

		public void run() {
			isMoving = true;
			try {
				if (dir < 0) {
					currImageX = 0;
					nextImageX = width;// * 2;
					int tmp = width;
					System.out.println("Thread Count " + Thread.activeCount());
					for (; tmp > 0;) {
						tmp = tmp + dir;// --;
						currImageX = currImageX + dir;// 10;// --;
						nextImageX = nextImageX + dir;// 10;// --;
						if (nextImageX < 0)
							nextImageX = 0;
						reDrawWidget();
						Thread.sleep(10);
					}
					isMoving = false;
					checkIndex();
					// check();
				} else {

					currImageX = 0;
					nextImageX = -width;
					int tmp = 0;
					for (; tmp < width;) {
						tmp = tmp + dir;
						currImageX = currImageX + dir;// 10;// --;
						nextImageX = nextImageX + dir;// 10;// --;
						if (currImageX > width)
							currImageX = width + 1;
						reDrawWidget();
						Thread.sleep(10);
					}
					isMoving = false;
					checkIndex2();
					// check();
				}
			} catch (InterruptedException e) {

			}
			System.out.println("After Scrolling : Current  Index " + currentIndex + " Next index " + nextIndex);
			currImageX = 0;
		}

		// public void check() {
		// if (dir < 0) {// move left increse
		// currentIndex++;
		// nextIndex = currentIndex + 1;
		// } else {
		// currentIndex--;
		// nextIndex = currentIndex - 1;
		// }
		// }
	}

	private int currentIndex = 0;
	private volatile int currImageX;
	private volatile int currImageY;
	private final Vector images;
	private volatile boolean isMoving;
	private volatile int nextImageX;
	private volatile int nextImageY;

	private volatile int nextIndex;

	private Timer timer;

	/**
	 * 
	 * @param images
	 *            need to be of equal dimensions
	 * @param x
	 * @param y
	 * @throws NullPointerException
	 *             if <code>images</code> is null.
	 */
	public SlidingPictureBrowser(Theme theme) {
		super(theme, 0, 0);
		images = new Vector();
	}

	public void addImage(Image image) {
		if (images.isEmpty()) {
			width = image.getWidth();
			height = image.getHeight();
		}
		images.addElement(image);
		reDrawWidget();
	}

	public void cleanUp() {
		// picMeta = null;
		images.removeAllElements();
	}

	private void move(byte i) {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		// if (i < 0) {// move left
		// if (currentIndex < images.length - 1) {
		// currentIndex++;
		// nextIndex = currentIndex + 1;
		// } else {
		// currentIndex = 0;
		// currentIndex = images.length - 1;
		// }
		//
		// } else {// move right
		// if (currentIndex > 0) {
		// currentIndex--;
		// }
		// }

		timer = new Timer();
		timer.schedule(new Ani(i), 0);
	}

	public boolean moveLeft() {
		System.out.println("Current  Index " + currentIndex + " Next index " + nextIndex);
		move((byte) -10);
		return true;
	}

	public boolean moveRight() {
		System.out.println("Current  Index " + currentIndex + " Next index " + nextIndex);
		move((byte) 10);
		return true;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		if (!images.isEmpty()) {
			g.drawImage((Image) images.elementAt(currentIndex), currImageX, currImageY, 0);
			if (isMoving)
				g.drawImage((Image) images.elementAt(nextIndex), nextImageX, nextImageY, 0);
		}
		release(g, width, height, false);

	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		switch (dir) {
		case Canvas.LEFT:
			if (location == IN) {
				moveLeft();
			} else
				return false;
			break;
		case Canvas.RIGHT:
			if (location == IN) {
				moveRight();
			} else {
				return false;
			}
			break;
		case Canvas.UP:
			if (location == LOWER) {
				location = IN;
			} else if (location == IN) {
				location = UPPER;
				return false;
			}
		case Canvas.DOWN:
			if (location == UPPER)
				location = IN;
			else if (location == IN) {
				location = LOWER;
				return false;
			}

		default:
			return false;
		}
		return true;
	}

	protected void updateModel() {
	// TODO Auto-generated method stub

	}
}
